<?php
/**
 * Created by Jayli.
 * User: Administrator
 * Date: 2018-03-07
 * Time: 14:46
 */

namespace App\Http\Controllers\Web\Member\Buyer;

use App\Http\Controllers\Web\BaseController;
use Illuminate\Http\Request;

class InvoicesController extends BaseController
{
    protected $api_url = [
        'index'  => ['get',   'buyer/invoices'],
        'show'   => ['get',   'buyer/invoices/%d'],
        'store'  => ['post',  'buyer/invoices'],
        'update' => ['put',   'buyer/invoices/%d'],
        'delete' => ['delete','buyer/invoices/%d'],
        'check'  => ['post',  'buyer/invoices/check'],
    ];

    protected $api_host;

    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->api_host = config('url.api_web');
    }

    public function index()
    {
        $type = $this->request->get('type', 'list');
        //请求部分-发票列表
        $invoices = http_web($this->api_url['index'])->send();

        if (!($invoices['http_code'] == 200 && $invoices['code'] == 0)) {

        }

        if ($type == 'list') {
            return view('web.user.member.invoice_list', $invoices['data']);
        } else {
            $personal_invoice = null;
            $company_regular_invoice = null;
            $company_special_invoice = null;
            foreach ($invoices['data']['list'] as $invoice) {
                if ($invoice['invoice_message'] == 1) {// 个人发票
                    $personal_invoice = $invoice;
                } elseif ($invoice['invoice_message'] == 2 && $invoice['invoice_type'] == 1) {
                    // 企业发票 普通发票
                    $company_regular_invoice = $invoice;
                } elseif ($invoice['invoice_message'] == 2 && $invoice['invoice_type'] == 2) {
                    // 企业发票 专用发票
                    $company_special_invoice = $invoice;
                }
            }
            $current_type = $this->request->get('current_type','company_regular_invoice');
//            dd(compact('personal_invoice', 'company_regular_invoice', 'company_special_invoice'));
            return view('web.order.component.invoices_select', compact('personal_invoice', 'company_regular_invoice', 'company_special_invoice','current_type'));
        }
    }
    /**
     * 发票新增页面
     * method get
     * url /create
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        return view('web.user.member.invoice',['request_url'=>'','invoice'=>'']);
    }

    public function show()
    {
        // 无详情页
    }

    /**
     * 创建、修改 发票-form提交
     * method post
     * url /
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function store()
    {
        //验证部分
        $data = $this->form();
        $id = $this->request->get('id',null);

        if ($id) {
            return $this->ajaxUpdate($id, $data);
        }else{
            //请求部分
            $return = http_web($this->api_url['store'])->send($data);
            if ($return['code']>0) {
                return response()->json(['code'=>$return['code'],'message'=>$return['message']]);
            }
            //跳转至列表页，如果有详情页，跳转至详情页
            return $return;
        }
    }

    public function edit($id)
    {
        // 查询单条
        $ret = http_web($this->api_url['show'],[$id])->send();
        return view('web.user.member.invoice',['invoice'=>$ret['data']]);
    }
    // 更新 默认
    public function update($id)
    {
        //
        //请求部分
        $ret = http_web($this->api_url['update'], [$id])->send();

        //结果处理部分
        return $ret;
    }

    //update公共方法
    private function ajaxUpdate($id, $data)
    {
        $ret = http_web($this->api_url['store'],[$id])->send($data);

        return $ret;
    }

    public function destroy($id)
    {
        $ret = http_web($this->api_url['delete'],[$id])->send();
        return $ret;
    }
    /**
     * 创建、更新公共验证部分
     * @return array
     */
    private function form()
    {
        $invoice_message = $this->request->get('invoice_message',1);
        if ($invoice_message!=1) {
            $this->validate($this->request, [
                'id' => 'integer',
                'invoice_message' => 'required|integer|in:1,2',
                'invoice_type' => 'required|integer|in:1,2',
                'invoice_title' => 'required|string',
                'tax_code' => 'required_if:invoice_message,2|string',
                'invoice_address' => 'required_if:invoice_message,2|string',
                'invoice_tel' => 'required_if:invoice_message,2|string',
                'invoice_bank' => 'required_if:invoice_message,2|string',
                'invoice_account' => 'required_if:invoice_message,2|string',
                'post_address' => 'required_if:invoice_message,2|string',
                'post_contacts' => 'required_if:invoice_message,2|string',
                'post_tel' => 'required_if:invoice_message,2|string',
                'remark' => 'string',
            ]);
        }else{
            $this->validate($this->request, [
                'invoice_title' => 'required',
            ],
            [
                'invoice_title'=>'发票抬头不可为空',
            ]);
        }
        return $this->request->all();
    }

    /**
     * ajax 验证是否可以继续添加发票
     * @return array
     */
    public function checkInvoices(){
        $data = [];
        $ret = http_web($this->api_url['check'])->send($data);
        return response()->json($ret);
    }    

}